<?php
namespace Home\Controller;

use Think\Controller;

class IndexController extends CommonController
{
    public function _initialize()
    {
        parent::_initialize();
        Vendor('Wxmpay.Pubhelper');
        Vendor('Wxmpay.Rexception');
        $this->wechatApi = D('WechatApi');
    }

    /**
     * 销售路由
     */
    public function index()
    {
        $jsApi = new \JsApi_pub();
        if (empty($_SESSION['wechat_openid']) && isWXBrowser()) {
            if (!isset($_GET['code'])) {
                $url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
                $url = $jsApi->createAuthUserUrlForCode(urlencode($url));
                sys_debug_log("***********index createAuthUserUrlForCode***********************");
                sys_debug_log($url);
                Header("Location: $url");
            } else {
                $code = $_GET['code'];
                $wat = $this->wechatApi->getWebAccessToken($code);
                if (!empty($wat['access_token']) && !empty($wat['openid'])) {
                    $this->wechatApi->getSnsapiUserinfo($wat['access_token'], $wat['openid']);
                }
            }
        }
        if (isset($_GET['code'])){
            $code = $_GET['code'];
            $wat = $this->wechatApi->getWebAccessToken($code);
            if (!empty($wat['access_token']) && !empty($wat['openid'])) {
                $this->wechatApi->getSnsapiUserinfo($wat['access_token'], $wat['openid']);
            }
        }

        // 微信联合登录
        if(!empty($_SESSION['wechat_openid']) && I('session.is_login',false)==false){
            $wechatUserInfo = D('WechatUserInfo');
            $wxinfo = $wechatUserInfo->where("openid = '".$_SESSION['wechat_openid']."'")->find();
            if(!empty($wxinfo['user_id'])){
                $this->doLogin($wxinfo['user_id']);
            }
        }
        $this->display();
    }

    //测试页面
    public function test()
    {
        $this->display();
    }

    // 首页
    public function home()
    {
        $this->display('Home/index');
    }

    //购物车
    public function cart()
    {
        $this->display('Cart/index');
    }

    //密码登录
    public function pwd_login()
    {
        $this->display('pwd_login');
    }

    //登录页
    public function login()
    {
        $this->display('login');
    }

    //团购页
    public function gpon()
    {
        $this->display('Groupon/gpon');
    }

    //详情页
    public function detail()
    {
        $this->display('Detail/detail');
    }

    //通用的广告模板页面

    public function tuixian() {
        $this->display('Tuixian/tuixian');
    }

    public function category() {
        $this->display('Category/category');
    }


    /**
     * 交易路由
     */
    public function trade()
    {
        if (!$this->checklogin()) {
            $this->error('您所访问的页面不存在！');
        }
        $this->display();
    }

    //快递列表页
    public function express()
    {
        $this->display('Express/index');
    }

    //订单操作页面
    public function expressOp()
    {
        $this->display('Express/operation');
    }

    //快递添加页
    public function expressadd()
    {
        $this->display('Express/add');
    }

    //快递修改页
    public function expressmodify()
    {
        $this->display('Express/modify');
    }

    //订单页
    public function order()
    {
        if (!$this->checklogin()) {
            $this->error('您所访问的页面不存在！');
        }
        $this->display('Order/index');
    }

    //支付页
    public function pay()
    {
        if (!$this->checklogin()) {
            $this->error('您所访问的页面不存在！');
        }
        $this->display('Order/pay');
    }

    /**
     * 账户路由
     */
    public function center()
    {
        $userInfo = D('UserInfo');
        $head_img = $userInfo->getHeadImg($this->uId());
        $this->assign('head_img', $head_img);
        $this->display('Account/center');
    }

    public function center_setting()
    {
        $this->display('Account/center_setting');
    }

    public function center_setting_pwdmodify() {
        $this->display('Account/center_setting_pwdmodify');
    }

    public function ordercenter()
    {
        $this->display('Account/ordercenter');
    }
	
	public function yuxian(){
		$this->display('Test/yuxian');
	}
	public function musenindex(){
		$this->display('Test/musenindex');
	}
	public function musensifang(){
		$this->display('Test/musensifang');
	}
	public function lemi(){
		$this->display('Test/lemi');
	}
    // 404
    public function notfound()
    {
        $this->error('您所访问的页面不存在, 将跳转至首页！', '/');
        exit();
    }
}

;